package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;


/**
 * @author Xiao
 * @version 1.0
 * @description 地理位置数据接口实现类
 * @date 2021/10/31
 */
@DubboService
public class UserLocationImpl implements UserLocationApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean setLocation(Double latitude, Double longitude, String addrStr, Long userId) {
        try {
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
            GeoJsonPoint point = new GeoJsonPoint(longitude, latitude);
            if (ObjectUtil.isNotEmpty(userLocation)) {
                Update update = Update.update("location", point)
                        .set("updated", System.currentTimeMillis())
                        .set("lastUpdated", userLocation.getUpdated())
                        .set("address", addrStr);
                mongoTemplate.updateFirst(query, update, UserLocation.class);
            } else {
                userLocation = new UserLocation();
                userLocation.setUserId(userId);
                userLocation.setLocation(point);
                userLocation.setAddress(addrStr);
                userLocation.setCreated(System.currentTimeMillis());
                userLocation.setUpdated(System.currentTimeMillis());
                userLocation.setLastUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLocation);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Long> search(Long userId, Double distance) {
        //根据用户id，查询用户的位置信息
        Criteria criteria = Criteria.where("userId").is(userId);
        UserLocation userLocation = mongoTemplate.findOne(Query.query(criteria), UserLocation.class);
        if (ObjectUtil.isEmpty(userLocation)) {
            return null;
        }
        //已当前用户位置绘制原点
        GeoJsonPoint point = userLocation.getLocation();
        //绘制半径
        Distance kmDistance = new Distance(distance/1000, Metrics.KILOMETERS);
        //绘制圆形
        Circle circle = new Circle(point, distance);
        //查询
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = mongoTemplate.find(query, UserLocation.class);
        return CollUtil.getFieldValues(userLocations, "userId", Long.class);
    }
}
